/* 	This project is about taking the picture and identifying the color so that the color blind people can 
 	identify the color. I have used forums to use the camera from a number of posts.
 	The image is taken through the camera, and sent to the processing unit so that it can display the image of the points selected 
	and the can help the disabled person to recognize the colors which he would have not seen.
	Author : Kanisk Utsav  
	Date : 28-02-2010
	Platform : Android on HTC.
	 
*/

// Packages included for the project

package ss12.MobileColorIdentifier;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import ss12.MobileColorIdentifier.Image.GPU;
import ss12.MobileColorIdentifier.speak.SpeakAndroid;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;



public class ColorIdentifier extends Activity implements SurfaceHolder.Callback{
	/** Called when the activity is first created. */
	// Surface View object created.
	private SurfaceView mSurfaceView;
	
	// Surface Holder object created.
	private SurfaceHolder mSurfaceHolder;
	
	// Camera object created.
	private Camera mCamera;
	
	// boolean to test if camera running.
	boolean mPreviewRunning = false;

	// Private Button searchButton;
	private SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyyMMddHHmmssSS");  
	private Uri target = Media.EXTERNAL_CONTENT_URI;
	//private static final int GALLERY_ID = Menu.FIRST;
	private static final int CAMERA_ID = Menu.FIRST;
	private static final int IMAGE_ID = Menu.FIRST + 1;
	private static final int SPEAK_ID = Menu.FIRST + 2;
	private static final int ACTIVITY_GALLERY=0;
	private static final int ACTIVITY_IMAGE=1;
	private static final int ACTIVITY_SPEAK=2;
	private GPU gpuUnit;
	public static byte[] dataByte;
	private Bundle bundle;
	
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		
		//android.view.IWindowManager windowService = android.view.IWindowManager.Stub.asInterface(ServiceManager.getService("window")); 
		//windowService.setOrientation(1);
		//Activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		//Activity.getRequestedOrientation();
		// initializes the object 
		gpuUnit = new GPU();
		Log.e(getClass().getSimpleName(), "onCreate coooola");  

		//
		setContentView(R.layout.main);
		getWindow().setFormat(PixelFormat.TRANSLUCENT); 
		setContentView(R.layout.image_layout);
		mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
		mSurfaceHolder = mSurfaceView.getHolder();
		mSurfaceHolder.addCallback(this);
		mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

		// searchButton = (Button) findViewById(R.id.search);
		// searchButton.setOnClickListener(new View.OnClickListener() {

		//   public void onClick(View view) {
		//sendRequestForDirections();
		//mCamera.takePicture(shutter, raw, jpeg)();

		//Intent intent = new //(Intent.ACTION_VIEW, target);  
		//startActivity(intent);  
		//return true;  
		// }
		//});
	}

	@Override  
	protected void onRestoreInstanceState(Bundle savedInstanceState)  
	{  
		super.onRestoreInstanceState(savedInstanceState);  
	}  

	public boolean onCreateOptionsMenu(android.view.Menu menu) {  
//		MenuItem item = menu.add(0, GALLERY_ID, 0, "goto gallery"); 
		//MenuItem item = menu.add(0, CAMERA_ID, 0, "goto camera");
		//MenuItem item_image = menu.add(0, IMAGE_ID, 0, "goto Image");
		//MenuItem item_speak = menu.add(0, SPEAK_ID, 0, "Speak");
		return true;  
	}  

	public static byte[] getByte()
	{
		return dataByte;
	}
	
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch(item.getItemId()) {
		//case GALLERY_ID:
		//	gallery();
		//	return true;
		case CAMERA_ID:
			camera();
			return true;
		case IMAGE_ID:
			img();
			// ???????????????????????
		case SPEAK_ID:
			speak();
			return true;
		}
		return super.onMenuItemSelected(featureId, item);
	}

	public void camera(){
		Intent intent = new Intent(Intent.ACTION_VIEW, target);
	}
	public void gallery()
	{
		Intent intent = new Intent(Intent.ACTION_VIEW, target);  
		startActivity(intent);
	}

	public void img()
	{
		Intent i = new Intent(this, GPU.class);
		startActivityForResult(i, ACTIVITY_IMAGE);
	}

	public void speak()
	{
		Intent i = new Intent(this, SpeakAndroid.class);
		startActivityForResult(i, ACTIVITY_SPEAK);
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
		if (mPreviewRunning) {
			mCamera.stopPreview();
		}
		Camera.Parameters p = mCamera.getParameters();
		p.setPreviewSize(w, h);
		p.set("rotation", 1);
		mCamera.setParameters(p);
		try {
			mCamera.setPreviewDisplay(holder);
		} catch (IOException e) {
			e.printStackTrace();
		}
		mCamera.startPreview();
		mPreviewRunning = true;
	}

	public void surfaceCreated(SurfaceHolder arg0) {
		mCamera = Camera.open();
	}

	public void surfaceDestroyed(SurfaceHolder arg0) {
		mCamera.stopPreview();
		mPreviewRunning = false;
		mCamera.release();
	}

	public boolean onKeyDown(int keyCode, KeyEvent event)  
	{  
		ImageCaptureCallback iccb = null;  
		if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {  
			try {  
				String filename = timeStampFormat.format(new Date());  
				ContentValues values = new ContentValues();  
				values.put(Media.TITLE, filename);  
				values.put(Media.DESCRIPTION, "Image capture by camera");  
				Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);  
				//String filename = timeStampFormat.format(new Date());  
				iccb = new ImageCaptureCallback( getContentResolver().openOutputStream(uri));  
			} catch(Exception ex ){  
				ex.printStackTrace();  
				Log.e(getClass().getSimpleName(), ex.getMessage(), ex);  
			}  
		}  
		if (keyCode == KeyEvent.KEYCODE_BACK) {  
			return super.onKeyDown(keyCode, event);  
		}
		if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {  
			mCamera.takePicture(null, null, mPictureCallbackJpeg);
			// mCamera.takePicture(shutter, raw, jpeg)
			return true;  
		}
		return false;  
	} 

	Camera.PictureCallback mPictureCallbackRaw = new Camera.PictureCallback() {  
		public void onPictureTaken(byte[] data, Camera c) {  
			//Log.e(getClass().getSimpleName(), "PICTURE CALLBACK RAW: " + data);  
			//String hexString = gpuUnit.processImg(data);
			//Log.e(getClass().getSimpleName(), "PICTURE CALLBACK RAW: " + hexString);
			//mCamera.startPreview();  
		}
	};

	Camera.PictureCallback mPictureCallbackJpeg= new Camera.PictureCallback() {  
		public void onPictureTaken(byte[] data, Camera c) {  
			//Log.e(getClass().getSimpleName(), "PICTURE CALLBACK JPEG: data.length = ");
			//Log.e(getClass().getSimpleName(), "PICTURE CALLBACK RAW: " + data); 
			//String hexString = gpuUnit.processImg(data);
			dataByte = data;
			
			img();
			
			//Log.e(getClass().getSimpleName(), "PICTURE CALLBACK RAW: " + hexString);
		}
	};

	Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() {  
		public void onShutter() {  
			Log.e(getClass().getSimpleName(), "SHUTTER CALLBACK"); 
		}  
	};

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//super.onCreate(bundle);
	}
	
	
}
